package ru.nsu.ccfit.shumkina.algorithm.adaptation;

import ru.nsu.ccfit.shumkina.algorithm.graphworks.Graph;
import ru.nsu.ccfit.shumkina.algorithm.logworks.Logs;
import ru.nsu.ccfit.shumkina.algorithm.logworks.Session;
import ru.nsu.ccfit.shumkina.algorithm.world.Settings;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Вера
 * Time: 3:23
 * To change this template use File | Settings | File Templates.
 */
public class PathsInfoCollector {
    public static TreeMap<String, UsedPathsInfo> collectInfo(Logs logs, Settings settings, Graph menu)
    {
        TreeMap<String, UsedPathsInfo> startPagesMap = new TreeMap<String, UsedPathsInfo>();

        Session session = logs.getNextSession();
        ArrayList<String> finalPages = new ArrayList<String>();
        while (session != null)
        {
            session.clearPath();
            // check if path present in original graph
            {
                 for (int i = 1; i < session.userFullPath.size(); i++)
                 {
                     if (!menu.arcNames.
                             containsKey(session.userFullPath.get(i - 1) + " -> " + session.userFullPath.get(i)))
                     {
                         session = logs.getNextSession();
                         continue;
                     }
                 }
            }
            if (session.userFullPath.size() > settings.getAdaptationLengthThreshold())
            {
                if (!startPagesMap.containsKey(session.userFullPath.get(0)))
                {
                    startPagesMap.put(session.userFullPath.get(0), new UsedPathsInfo(session.userFullPath.get(0)));
                }

                UsedPathsInfo upi = startPagesMap.get(session.userFullPath.get(0));
                if (upi.paths.containsKey(session.userFullPath.get(session.userFullPath.size() - 1)))
                {
                    Path p = upi.paths.get(session.userFullPath.get(session.userFullPath.size() - 1));
                    p.useCount++;
                }
                else
                {
                    Path p = new Path();
                    p.useCount = 1;
                    p.adaptedPath = null;
                    p.finalPageId =  session.userFullPath.get(session.userFullPath.size() - 1);
                    p.path = session.userFullPath;
                    upi.paths.put(p.finalPageId, p);
                    finalPages.add(p.finalPageId);
                }
            }
            session = logs.getNextSession();
        }

        //delete paths with non-final final pages
        ArrayList<String> pathsToDelete = new ArrayList<String>();
        for (Map.Entry<String, UsedPathsInfo> entry: startPagesMap.entrySet())
        {
            for (Map.Entry<String, Path> pathEntry: entry.getValue().paths.entrySet())
            {
                for (String fp: finalPages)
                {
                    if (pathEntry.getValue().path.indexOf(fp) != -1 &&
                            pathEntry.getValue().path.indexOf(fp) != pathEntry.getValue().path.size() - 1 &&
                            pathsToDelete.indexOf(fp) == -1)
                        pathsToDelete.add(fp);
                }
            }
            while(!pathsToDelete.isEmpty())
            {
                entry.getValue().paths.remove(pathsToDelete.get(0));
                pathsToDelete.remove(0);
            }
        }
        for (Map.Entry<String, UsedPathsInfo> entry: startPagesMap.entrySet())
        {
            entry.getValue().startPageIdUseCount = 0;
            for (Map.Entry<String, Path> pathEntry: entry.getValue().paths.entrySet())
            {
                entry.getValue().startPageIdUseCount += pathEntry.getValue().useCount;
            }
        }
        //+themes settings
        return  startPagesMap;
    }
}
